home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Mac Parts / Views / Canvas / Canvas.cp next >
Text File  |  2000-06-23  |  6KB  |  323 lines

  1. // Canvas.cp
  2.  
  3. #ifndef Canvas_h
  4. #include "Canvas.h"
  5. #endif
  6. #ifndef GrafPortObject_h
  7. #include "GrafPortObject.h"
  8. #endif
  9. #ifndef WindowObject_h
  10. #include "WindowObject.h"
  11. #endif
  12. #ifndef CanvasMaintainer_h
  13. #include "CanvasMaintainer.h"
  14. #endif
  15.  
  16. Canvas::Canvas()
  17.   : PortMap( Rectangle32::zero, Rectangle::zero ),
  18.      port( 0 ),
  19.      window( 0 )
  20.   {
  21.   }
  22.  
  23. Canvas::Canvas( GrafPortObject& thePort )
  24.   : PortMap( PortMap::Identity( thePort.LocalBounds() ) ),
  25.      port( &thePort ),
  26.      window( 0 ),
  27.      clip( thePort.VisibleRegion() )
  28.   {
  29.     PortMap::RestrictOnScreen( clip.Bounds() );
  30.   }
  31.  
  32. Canvas::Canvas( WindowObject& window )
  33.   : PortMap( PortMap::Identity( window.Port().LocalBounds() ) ),
  34.      port( &window.Port() ),
  35.      window( &window ),
  36.      clip( window.Port().VisibleRegion() )
  37.   {
  38.     PortMap::RestrictOnScreen( clip.Bounds() );
  39.   }
  40.  
  41. Canvas::Canvas( const Canvas& source )
  42.   : PortMap( source ),
  43.      port( source.port ),
  44.      window( source.window ),
  45.      clip( source.clip )
  46.   {
  47.   }
  48.  
  49. void Canvas::operator=( GrafPortObject& thePort )
  50.   {
  51.     Map() = PortMap::Identity( thePort.LocalBounds() );
  52.      port = &thePort;
  53.      window = 0;
  54.      clip = thePort.VisibleRegion();
  55.     PortMap::RestrictOnScreen( clip.Bounds() );
  56.   }
  57.  
  58. void Canvas::operator=( WindowObject& theWindow )
  59.   {
  60.     operator=( theWindow.Port() );
  61.      window = &theWindow;
  62.   }
  63.  
  64. bool Canvas::operator==( const Canvas& right ) const
  65.   {
  66.     return port == right.port && clip == right.clip;
  67.   }
  68.  
  69. void Canvas::RestrictOnScreen( Rectangle r )
  70.   {
  71.     PortMap::RestrictOnScreen( r );
  72.     clip &= OnScreen();
  73.   }
  74.  
  75. void Canvas::RestrictOnScreen( const RegionObject& r )
  76.   {
  77.     clip &= r;
  78.     PortMap::RestrictOnScreen( clip.Bounds() );
  79.   }
  80.  
  81. void Canvas::RestrictOffScreen( Rectangle32 r )
  82.   {
  83.     PortMap::RestrictOffScreen( r );
  84.     clip &= OnScreen();
  85.   }
  86.  
  87. void Canvas::RestrictToValid()
  88.   {
  89.     Assert( HasPort() );
  90.     if ( HasWindow() )
  91.       {
  92.         PointObject globalToLocal( window->Port().GlobalToLocal() );
  93.          clip -= globalToLocal;
  94.          clip -= window->InvalidRegion();
  95.          clip += globalToLocal;
  96.         PortMap::RestrictOnScreen( clip.Bounds() );
  97.       }
  98.   }
  99.  
  100. void Canvas::RestrictToInvalid()
  101.   {
  102.     Assert( HasPort() );
  103.     if ( HasWindow() )
  104.       {
  105.         PointObject globalToLocal( window->Port().GlobalToLocal() );
  106.          clip -= globalToLocal;
  107.          clip &= window->InvalidRegion();
  108.          clip += globalToLocal;
  109.         PortMap::RestrictOnScreen( clip.Bounds() );
  110.       }
  111.      else
  112.       {
  113.         PortMap::RestrictOnScreen( Rectangle::zero );
  114.         clip.BeEmpty();
  115.       }
  116.   }
  117.  
  118. void Canvas::Submap( Rectangle32 r )
  119.   {
  120.     PortMap::Submap( r );
  121.     clip &= OnScreen();
  122.   }
  123.  
  124. void Canvas::SliceHorizontally( Range32 verticalRange )
  125.   {
  126.     PortMap::SliceHorizontally( verticalRange );
  127.     clip &= OnScreen();
  128.   }
  129.  
  130. void Canvas::SliceVertically( Range32 horizontalRange )
  131.   {
  132.     PortMap::SliceVertically( horizontalRange );
  133.     clip &= OnScreen();
  134.   }
  135.  
  136. void Canvas::ObstructOnScreen( Rectangle r )
  137.   {
  138.     clip -= r;
  139.     PortMap::RestrictOnScreen( clip.Bounds() );
  140.   }
  141.  
  142. void Canvas::ObstructOnScreen( const RegionObject& r )
  143.   {
  144.     clip -= r;
  145.     PortMap::RestrictOnScreen( clip.Bounds() );
  146.   }
  147.  
  148. void Canvas::ObstructOffScreen( Rectangle32 r )
  149.   {
  150.     clip -= ToScreen( r );
  151.     PortMap::RestrictOnScreen( clip.Bounds() );
  152.   }
  153.  
  154. void Canvas::GetValidRegion( RegionObject& result ) const
  155.   {
  156.     result = clip;
  157.  
  158.     if ( HasWindow() )
  159.       {
  160.         PointObject globalToLocal( window->Port().GlobalToLocal() );
  161.          result -= globalToLocal;
  162.          result -= window->InvalidRegion();
  163.          result += globalToLocal;
  164.        }
  165.   }
  166.  
  167. void Canvas::GetInvalidRegion( RegionObject& result ) const
  168.   {
  169.     if ( HasWindow() )
  170.       {
  171.         PointObject globalToLocal( window->Port().GlobalToLocal() );
  172.         result = clip;
  173.          result -= globalToLocal;
  174.          result &= window->InvalidRegion();
  175.          result += globalToLocal;
  176.        }
  177.       else
  178.          result.BeEmpty();
  179.   }
  180.  
  181. RegionObject& Canvas::TemporaryRegion()
  182.   {
  183.     static RegionObject region;
  184.     return region;
  185.   }
  186.  
  187. RegionObject& Canvas::ClippedRegion( Rectangle r ) const
  188.   {
  189.     RegionObject& temporary( TemporaryRegion() );
  190.     temporary = r;
  191.     temporary &= clip;
  192.     return temporary;
  193.   }
  194.  
  195. RegionObject& Canvas::ClippedRegion( const RegionObject& r ) const
  196.   {
  197.     RegionObject& temporary( TemporaryRegion() );
  198.     temporary = r;
  199.     temporary &= clip;
  200.     return temporary;
  201.   }
  202.  
  203. void Canvas::Validate() const
  204.   {
  205.     Assert( HasPort() );
  206.     
  207.     if ( HasWindow() )
  208.       {
  209.         Assert( port->IsCurrent() );
  210.         ValidRgn( clip );
  211.       }
  212.   }
  213.  
  214. void Canvas::ValidateOnScreen( const RegionObject& r ) const
  215.   {
  216.     Assert( HasPort() );
  217.     
  218.     if ( HasWindow() )
  219.       {
  220.         Assert( port->IsCurrent() );
  221.         ValidRgn( ClippedRegion( r ) );
  222.       }
  223.   }
  224.  
  225. void Canvas::ValidateOnScreen( Rectangle r ) const
  226.   {
  227.     Assert( HasPort() );
  228.     
  229.     if ( HasWindow() )
  230.       {
  231.         Assert( port->IsCurrent() );
  232.         ValidRgn( ClippedRegion( r ) );
  233.       }
  234.   }
  235.  
  236. void Canvas::ValidateOffScreen( Rectangle32 r ) const
  237.   {
  238.     Assert( HasPort() );
  239.     
  240.     if ( HasWindow() )
  241.       {
  242.         Assert( port->IsCurrent() );
  243.         ValidRgn( ClippedRegion( ToScreen( r ) ) );
  244.       }
  245.   }
  246.  
  247. void Canvas::Invalidate() const
  248.   {
  249.     Assert( HasPort() );
  250.     Assert( HasWindow() );
  251.     
  252.     if ( HasWindow() )
  253.       {
  254.         Assert( port->IsCurrent() );
  255.         InvalRgn( clip );
  256.       }
  257.   }
  258.  
  259. void Canvas::InvalidateOnScreen( const RegionObject& r ) const
  260.   {
  261.     Assert( HasPort() );
  262.     Assert( HasWindow() );
  263.     
  264.     if ( HasWindow() )
  265.       {
  266.         Assert( port->IsCurrent() );
  267.         InvalRgn( ClippedRegion( r ) );
  268.       }
  269.   }
  270.  
  271. void Canvas::InvalidateOnScreen( Rectangle r ) const
  272.   {
  273.     Assert( HasPort() );
  274.     Assert( HasWindow() );
  275.     
  276.     if ( HasWindow() )
  277.       {
  278.         Assert( port->IsCurrent() );
  279.         InvalRgn( ClippedRegion( r ) );
  280.       }
  281.   }
  282.  
  283. void Canvas::InvalidateOffScreen( Rectangle32 r ) const
  284.   {
  285.     Assert( HasPort() );
  286.     Assert( HasWindow() );
  287.     
  288.     if ( HasWindow() )
  289.       {
  290.         Assert( port->IsCurrent() );
  291.         InvalRgn( ClippedRegion( ToScreen( r ) ) );
  292.       }
  293.   }
  294.  
  295. void Copy( const Canvas& source, const Canvas& destination )
  296.   {
  297.     CanvasMaintainer cm( destination );
  298.  
  299.     Rectangle32 sharedArea = source.OffScreen() & destination.OffScreen();
  300.  
  301.     Rectangle sourceArea = source.ToScreen( sharedArea );
  302.     Rectangle destinationArea = destination.ToScreen( sharedArea );
  303.  
  304.     PointObject distance = destinationArea.TopLeft() - sourceArea.TopLeft();
  305.         
  306.     RegionObject valid;
  307.     source.GetValidRegion( valid );
  308.     valid += distance;
  309.     
  310.     if ( distance != PointObject::zero )
  311.         CopyBits( &source.Port().portBits,        &destination.Port().portBits,
  312.                      &sourceArea,                        &destinationArea,
  313.                      srcCopy,
  314.                      valid );
  315.     
  316.     if ( destination.HasWindow() )
  317.       {
  318.         ValidRgn( valid );
  319.         valid.Complement( destination.Clip() );
  320.         InvalRgn( valid );
  321.       }
  322.   }
  323.